package com.zl.zlproject.base.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.gyf.barlibrary.ImmersionBar;
import com.zl.zlproject.base.application.AppManager;
import com.zl.zlproject.base.application.MyApplication;

import butterknife.ButterKnife;

/**
 * Created by 郑磊 on 2018/5/27.
 */

public abstract class BaseActivity extends AppCompatActivity {


    protected ImmersionBar mStatusBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this); //activity 任务栈管理
        setContentView(getContentViewRes());
        initBefore();
        ButterKnife.bind(this);
        mStatusBar = ImmersionBar.with(this);
        mStatusBar.init();
        initView();
        initData();
        initListerner();
    }


    /**
     * 初始化toolbar
     *
     * @param toolbar Toolbar
     */
    public void initToolbar(Toolbar toolbar, View backIcon, boolean showIvBack) {
        toolbar.setBackgroundResource(MyApplication.appColor);
       initStatusBar(toolbar);

        toolbar.setTitle("");
        if (backIcon != null) {
            backIcon.setVisibility(showIvBack ?View.VISIBLE :View.GONE);
           backIcon.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   finish();
               }
           });
        }

    }

    protected  void initStatusBar(Toolbar toolbar){
        mStatusBar.titleBar(toolbar).init();
    }



    protected void initView() {

    }


    protected void initListerner() {

    }

    protected void initData() {

    }

    /**
     * 初始化控件调用之前
     */
    protected void initBefore() {


    }



    protected abstract int getContentViewRes();




    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();  //沉侵式取消关联
        AppManager.getAppManager().finishActivity(this);
    }
}
